package com.weetop.cfw.mine.activity

import android.os.Bundle
import android.view.View
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.LogUtils
import com.google.android.material.tabs.TabLayout
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener
import com.weetop.cfw.R
import com.weetop.cfw.base.presenter.imp.AudioFrequencyPresenterImp
import com.weetop.cfw.base.ui.activity.CommonBaseActivity
import com.weetop.cfw.base.view.AudioFrequencyView
import com.weetop.cfw.bean.AudioVideoBrowsingRecordBean
import com.weetop.cfw.bean.AudioVideoFilterBean
import com.weetop.cfw.bean.TestBean
import com.weetop.cfw.callback.OnTabSelectedListenerImp
import com.weetop.cfw.constants.Constants
import com.weetop.cfw.home_page.activity.SearchActivity
import com.weetop.cfw.mine.adapter.MyCourseListAdapter
import com.weetop.cfw.utils.LogInUtils
import kotlinx.android.synthetic.main.activity_audio_frequency.*

/**
 * 音频列表界面
 */
class AudioFrequencyActivity : CommonBaseActivity(),TabLayout.OnTabSelectedListener,View.OnClickListener,AudioFrequencyView, OnRefreshLoadMoreListener {

    override fun onClick(p0: View?) {
        when(p0?.id){
            R.id.imageAudioFrequencyBack -> ActivityUtils.finishActivity(this)
            R.id.relativeSearch -> {
                Constants.audioClassId = classid
                SearchActivity.startSearchActivity(this@AudioFrequencyActivity,2)
            }
        }
    }

    private lateinit var myCourseListAdapter:MyCourseListAdapter

    private val audioVideoDataList = ArrayList<AudioVideoBrowsingRecordBean.DataBean>()

    private lateinit var audioFrequencyPresenterImp: AudioFrequencyPresenterImp

    private var currentPageNumber = 1//当前的数据页数
    private var totalCount = 0//数据的总数量
    private var classid = 0//筛选分类的ID，0表示无限

    private var isRefresh = false//是否正在刷新
    private var isLoadMore = false//是否加载更多

    override fun getLayoutId() = R.layout.activity_audio_frequency

    override fun initView(savedInstanceState: Bundle?) {
        audioFrequencyPresenterImp = AudioFrequencyPresenterImp()
        audioFrequencyPresenterImp.attachView(this)

        smartRefreshLayout.setOnRefreshLoadMoreListener(this)
        smartRefreshLayout.setEnableAutoLoadMore(false)
        setViewsOnClickListener(this,imageAudioFrequencyBack,relativeSearch)

    }

    private fun initTabLayout(audioVideoFilterBeanList:List<AudioVideoFilterBean.DataBean>){
        LogUtils.eTag("audioVideoFilterBeanList",audioVideoFilterBeanList)
        for(i in audioVideoFilterBeanList.indices){
            audioFrequencyIndicator.addTab(audioFrequencyIndicator.newTab().setText(audioVideoFilterBeanList[i].classname))
        }
        if(audioVideoFilterBeanList.isNotEmpty()){
            classid = audioVideoFilterBeanList[0].classid
        }
        audioFrequencyIndicator.addOnTabSelectedListener(object:OnTabSelectedListenerImp(){
            override fun onTabSelected(tab: TabLayout.Tab?) {
                classid = audioVideoFilterBeanList[audioFrequencyIndicator.selectedTabPosition].classid
                smartRefreshLayout.autoRefresh()
            }

            override fun onTabReselected(tab: TabLayout.Tab?) {
                super.onTabReselected(tab)
                smartRefreshLayout.autoRefresh()
            }
        })
        audioFrequencyPresenterImp.getAudioFrequencyDataList(this,classid,"",currentPageNumber)
    }

    override fun initData(savedInstanceState: Bundle?) {

        audioFrequencyPresenterImp.getAudioFrequencyFilterData(this)

        myCourseListAdapter = MyCourseListAdapter(R.layout.layout_item_audio_frequency,audioVideoDataList)
        myCourseListAdapter.emptyView = emptyView
        myCourseListAdapter.setOnItemClickListener { _, _, position ->
            if(LogInUtils.userIsLogIn()){
                AudioDetailActivity.startAudioDetailActivity(this,audioVideoDataList[position].infoid)
            }
        }
        audioFrequencyRV.adapter = myCourseListAdapter

    }

    override fun onTabReselected(tab: TabLayout.Tab?) {

    }

    override fun onTabUnselected(tab: TabLayout.Tab?) {

    }

    override fun onTabSelected(tab: TabLayout.Tab?) {

    }

    override fun onDestroy() {
        super.onDestroy()
        Constants.audioClassId = 0
        audioFrequencyPresenterImp.detachView()
    }

    /**
     * 管理音频筛选数据获取成功
     */
    override fun getAudioFrequencyFilterDataSuccess(audioVideoFilterBean: AudioVideoFilterBean?) {
        if(audioVideoFilterBean != null){
            val audioVideoDataBeanList = audioVideoFilterBean.data
            if(audioVideoDataBeanList.size == 0){
                audioFrequencyIndicator.visibility = View.GONE
            }else{
                audioFrequencyIndicator.visibility = View.VISIBLE
                initTabLayout(audioVideoDataBeanList)
            }
        }
    }

    /**
     * 管理音频数据列表获取成功
     */
    override fun audioFrequencyListDataGetSuccess(audioVideoBrowsingRecordBean: AudioVideoBrowsingRecordBean?) {
        if(audioVideoBrowsingRecordBean != null){
            totalCount = audioVideoBrowsingRecordBean.records

            if(isRefresh){
                audioVideoDataList.clear()
                audioVideoDataList.addAll(audioVideoBrowsingRecordBean.data)
                myCourseListAdapter.notifyDataSetChanged()
                smartRefreshLayout.finishRefresh(true)
                if(audioVideoBrowsingRecordBean.data.size > 0){
                    currentPageNumber = 1//将当前数据页数重置为1
                }
                isRefresh = false
                return
            }
            if(isLoadMore){
                audioVideoDataList.addAll(audioVideoBrowsingRecordBean.data)
                myCourseListAdapter.notifyDataSetChanged()
                smartRefreshLayout.finishLoadMore(false)
                if(audioVideoBrowsingRecordBean.data.size > 0){
                    currentPageNumber += 1//将当前数据页数增加1
                }
                isLoadMore = false
                return
            }
            audioVideoDataList.addAll(audioVideoBrowsingRecordBean.data)
            myCourseListAdapter.notifyDataSetChanged()
        }
    }

    /**
     * 管理音频数据列表获取失败
     */
    override fun audioFrequencyListDataGetFailed() {
        smartRefreshLayout.finishRefresh(false)
        smartRefreshLayout.finishLoadMore(false)
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        if(totalCount <= audioVideoDataList.size){
            showNativeShortToast("暂无更多数据")
            smartRefreshLayout.finishLoadMoreWithNoMoreData()
        }else{
            isLoadMore = true
            audioFrequencyPresenterImp.getAudioFrequencyDataList(this,classid,"",currentPageNumber + 1)
        }
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        isRefresh = true
        audioFrequencyPresenterImp.getAudioFrequencyDataList(this,classid,"",1)
    }
}
